package com.derbysoft.dswitch.adapter.ihg.webservice.handler;

import com.derbysoft.dswitch.adapter.ihg.common.domain.Direction;
import com.derbysoft.dswitch.adapter.ihg.common.log.LogDetailPair;
import com.derbysoft.dswitch.adapter.ihg.common.log.TransactionLogger;
import com.derbysoft.dswitch.adapter.ihg.common.utils.ErrorMessageUtils;
import com.derbysoft.dswitch.adapter.ihg.exception.InvokeProviderSideException;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.adapter.ihg.webservice.ProviderWebService;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.AccountProvider;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.RequestPair;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.ResponsePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by: jason
 * Date: 2012-05-22
 */
@Component("availWebServiceHandler")
public class AvailWebServiceHandlerImpl extends AbstractWebServiceHandler
        implements WebServiceHandler<OTAHotelAvailRQ, OTAHotelAvailRS> {

    @Autowired
    @Qualifier("availProviderWebService")
    private ProviderWebService<OTAHotelAvailRQ, OTAHotelAvailRS> availProviderWebService;

    @Override
    public OTAHotelAvailRS invoke(OTAHotelAvailRQ request, boolean serveReservation, AccountProvider accountProvider) {
        Set<LogDetailPair> logDetails = TransactionLogger.getLogDetails().get();
        if (logDetails == null) {
            logDetails = new HashSet<LogDetailPair>();
        }
        TransactionLogger.getLogDetails().set(logDetails);
        logDetails.add(LogDetailPair.build(request, Direction.TO_HOTEL));

        try {
            return availProviderWebService.invoke(request, accountProvider);
        } catch (Exception ex) {
            if (serveReservation) {
                TransactionLogger.getReservationLogger().get().setError(true);
            } else {
                TransactionLogger.getAvailLogger().get().setError(true);
            }
            throw new InvokeProviderSideException(ex.getMessage(),ex);
        }
    }

    @Override
    public List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> batchInvoke(List<RequestPair<OTAHotelAvailRQ>> requests,
                                                                            AccountProvider accountProvider) {
        Set<LogDetailPair> logDetails = TransactionLogger.getLogDetails().get();
        if (logDetails == null) {
            logDetails = new HashSet<LogDetailPair>();
        }
        TransactionLogger.getLogDetails().set(logDetails);
        for (RequestPair<OTAHotelAvailRQ> request : requests) {
            logDetails.add(LogDetailPair.build(request.getRequest(), Direction.TO_HOTEL));
        }
        try {
            return availProviderWebService.batchInvoke(requests, accountProvider);
        } catch (Exception ex) {
            TransactionLogger.getAvailLogger().get().setError(true);
            throw new InvokeProviderSideException(ex.getMessage(),ex);
        }
    }
}
